package artid.com.arttogo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.easeui.EaseConstant;
import com.hyphenate.easeui.controller.EaseUI;
import com.hyphenate.easeui.domain.EaseUser;
import artid.com.arttogo.view.easemob.EaseChatFragment;
import artid.com.arttogo.R;
import artid.com.arttogo.app.ArtToGoApp;
import artid.com.arttogo.network.AppUtil;

/**
 * 聊天页面，需要fragment的使用
 */
public class ChatActivity extends BaseActivity implements OnClickListener {
    public static ChatActivity activityInstance;
    String toChatUsername;
    public static EMMessage message;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.em_activity_chat);
		initView();
    }

	private void initView() {
		EaseChatFragment chatFragment = new EaseChatFragment();
		// 传入参数
		Bundle args = new Bundle();
		args.putInt(EaseConstant.EXTRA_CHAT_TYPE, EaseConstant.CHATTYPE_SINGLE);
		args.putString(EaseConstant.EXTRA_USER_ID, message.getUserName());
		chatFragment.setArguments(args);
		getSupportFragmentManager().beginTransaction().add(R.id.container, chatFragment).commit();
		mTittle = (TextView) findViewById(R.id.tittle_text);
		mBack = (ImageView) findViewById(R.id.back_btn);
		mTittle.setText("询价详情页");
		mBack.setOnClickListener(this);
		//设置头像
		ArtToGoApp.getInstance().easeUI.setUserProfileProvider(new EaseUI.EaseUserProfileProvider() {

			@Override
			public EaseUser getUser(String username) {
				// TODO Auto-generated method stub
				return getUserInfo(username);
			}
		});
	}

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.back_btn:
                finish();
                break;

            default:
                break;
        }
    }

    private EaseUser getUserInfo(String username) {
        EaseUser user = null;
        if (username.equals(EMClient.getInstance().getCurrentUser())) {
            //设置自己头像
            user = new EaseUser(username);
            user.setAvatar(AppUtil.getUser().getHeadurl());
            return user;
        } else {
            //设置好友头像
            user = new EaseUser(username);
            user.setAvatar(MainActivity.friendAvatar);
            return user;
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        activityInstance = null;
        MainActivity.friendAvatar = null;
        ArtToGoApp.mEMMessage = null;
        message=null;
    }

    @Override
    protected void onNewIntent(Intent intent) {
        // 点击notification bar进入聊天页面，保证只有一个聊天页面
        String username = message.getUserName();
        if (toChatUsername.equals(username))
            super.onNewIntent(intent);
        else {
            finish();
            startActivity(intent);
        }

    }

    @Override
    public void onBackPressed() {
        finish();
    }

    public String getToChatUsername() {
        return toChatUsername;
    }
}
